package cn.study.luckyapp.award.command;

import cm.study.config.util.AssertUtil;
import cn.study.luckyapp.assembler.AwardAssembler;
import cn.study.luckyclient.dto.AwardAddCmd;
import cn.study.luckyclient.dto.data.AwardVo;
import cn.study.luckydomain.award.AwardEntity;
import cn.study.luckydomain.gateway.AwardGateway;
import cn.study.luckydomain.gateway.PrizeGateway;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;

@AllArgsConstructor
@Component
public class AwardAddCmdExe {
    private final AwardGateway awardGateway;
    private final PrizeGateway prizeGateway;
    public AwardVo execute(AwardAddCmd cmd){
        AssertUtil.isTrue(Objects.isNull(cmd.getActivityId()),"奖项活动id为空");
        AwardEntity save = awardGateway.save(AwardAssembler.toEntity(cmd));
        if(!save.isPrize()){
            return  AwardAssembler.toAwardVo(save);
        }
       int update=prizeGateway.deductionInventory(cmd.getPrizeId(),cmd.getNumber());
        return AwardAssembler.toAwardVo(save);
    }
}
